// 字符串转byte
function stringToBytes(str) {
  var strArray = new Uint8Array(str.length);

  for (var i = 0; i < str.length; i++) {
    strArray[i] = str.charCodeAt(i);
  }

  const array = new Uint8Array(strArray.length);
  strArray.forEach((item, index) => array[index] = item);
  console.log(array.buffer);
  return array.buffer;
} // ArrayBuffer转16进制字符串示例


function ab2hext(buffer) {
  var hexArr = Array.prototype.map.call(new Uint8Array(buffer), function (bit) {
    return ('00' + bit.toString(16)).slice(-2);
  });
  return hexArr.join('');
} //16进制转字符串


function hexToString(str) {
  var trimedStr = str.trim();
  var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
  var len = rawStr.length;

  if (len % 2 !== 0) {
    // alert("Illegal Format ASCII Code!");
    return "";
  }

  var curCharCode;
  var resultStr = [];

  for (var i = 0; i < len; i = i + 2) {
    curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value

    resultStr.push(String.fromCharCode(curCharCode));
  }

  return resultStr.join("");
}
/*微信app版本比较*/


function versionCompare(ver1, ver2) {
  var version1pre = parseFloat(ver1);
  var version2pre = parseFloat(ver2);
  var version1next = parseInt(ver1.replace(version1pre + ".", ""));
  var version2next = parseInt(ver2.replace(version2pre + ".", ""));
  if (version1pre > version2pre) return true;else if (version1pre < version2pre) return false;else {
    if (version1next > version2next) return true;else return false;
  }
}

module.exports = {
	stringToBytes: stringToBytes,
	ab2hext: ab2hext,
	hexToString: hexToString,
	versionCompare: versionCompare
};